<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            ①：确定dp数组和下标含义
            dp[i][j]表示到第i，j个位置所有的不同的路径
            ②确定递推公式
            dp[i][j]=dp[i-1][j]+dp[i][j-1]
            ③dp数组初始化
            i==0时， dp[0][j]=1 dp[i][0]=1
            如果遍历时obstacleGrid[i][j]=1那么dp[i][j]=0
            ④确定遍历顺序
            从左往右
            
            
            */
            var uniquePathsWithObstacles = function (obstacleGrid) {
                let dp = new Array(obstacleGrid.length).fill().map(() => new Array(obstacleGrid[0].length).fill(0))
                for (let i = 0; i < dp.length; i++) {
                    if (obstacleGrid[i][0] == 1) {
                        //初始化时都是0
                        dp[i][0] = 0
                        break
                    } else {
                        dp[i][0] = 1
                    }
                }
                for (let j = 0; j < dp[0].length; j++) {
                    if (obstacleGrid[0][j] == 1) {
                        dp[0][j] = 0
                        break
                    } else {
                        dp[0][j] = 1
                    }
                }
                for (let i = 1; i < dp.length; i++) {
                    for (let j = 1; j < dp[0].length; j++) {
                        if (obstacleGrid[i][j] == 1) {
                            dp[i][j] = 0
                            continue
                        }
                        dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
                    }
                }
                return dp[dp.length - 1][dp[0].length - 1]
            }
            console.log(uniquePathsWithObstacles([[1, 0]]))
        </script>
    </body>
</html>
